package com.example.light.web.system.service;

import com.example.light.web.system.dao.RoleMapper;
import com.example.light.web.system.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    //查询角色信息
    public String QueryAll(Model model)
    {
        List<Role> roles=roleMapper.Queryall();
        model.addAttribute("roles",roles);
        return "system/role";
    }

    //添加角色界面
    private int TempId;//界面ID
    public String roleAddView(Model model, Integer roleId){
        if(null != roleId){
            TempId=roleId+1;
        }
        model.addAttribute("roleId",TempId);
        return "system/roleadd";
    }

    //添加角色
    public String roleAdd(Model model,Role role){
        Date date=new Date(System.currentTimeMillis());
        System.out.println(TempId);
        System.out.println(date);
        //department.setDeptId(TempId);
        role.setCreateTime(date);
        if(role.getRoleName()==""){
            return "f";
        }
        int flag=roleMapper.add(role);
        if(flag==1){
            return "t";
        }else{
            return "f";
        }
    }


    //角色修改界面
    private Role TempRole; //界面数据
    public String roleUpdateView(Model model,Role role){
        if(null != role.getRoleId()){
            TempRole=role;
        }
        model.addAttribute("roles",TempRole);
        return "system/roleupdate";
    }


    //角色修改
    public String roleUpdate(Model model,Role role){
        Date date=new Date(System.currentTimeMillis());
        role.setCreateTime(date);
        if(role.getRoleName()==""){
            return "t";
        }
        int flag=roleMapper.update(role);
        if(flag==1){
            return "t";
        }else{
            return "f";
        }
    }


    //删除角色（多个）
    public String roleDeletM(Model model,Integer [] ids){
        for(Integer id:ids){
            roleMapper.deleteByPrimaryKey(id);
        }
        //deptMapper.deletM(ids);
        List<Role> roles=roleMapper.Queryall();
        model.addAttribute("roles",roles);
        return "system/role";
    }
}
